//
//  LSYDetailViewController.m
//  contact
//
//  Created by lsy on 15/11/21.
//  Copyright © 2015年 刘世玉. All rights reserved.
//

#import "LSYDetailViewController.h"
#import <Masonry.h>
#import "LSYcoreDataManager.h"
#import "LSYplaceChooseView.h"

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件

#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件

#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件

@interface LSYDetailViewController ()<BMKMapViewDelegate>

{
    LSYplaceChooseView *_view;
    BMKMapView *_mapView;
}

@property(nonatomic, strong)LSYcoreDataManager *manager;
@end

@implementation LSYDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    _mapView.delegate = self;
}


- (void)viewWillDisappear:(BOOL)animated
{
    _mapView.delegate = nil;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.name = [[UITextField alloc] init];
        self.name.backgroundColor = [UIColor cyanColor];
        self.name.placeholder = @"姓名";
        
        self.sex = [[UITextField alloc] init];
        self.sex.placeholder = @"sex";
        self.sex.backgroundColor = [UIColor cyanColor];
        
        self.number = [[UITextField alloc] init];
        self.number.backgroundColor = [UIColor cyanColor];
        self.number.placeholder = @"学号";
        
        self.phoneNum = [[UITextField alloc] init];
        self.phoneNum.backgroundColor = [UIColor cyanColor];
        self.phoneNum.placeholder = @"电话";
        
        
        self.qq = [[UITextField alloc] init];
        self.qq.backgroundColor = [UIColor cyanColor];
        self.qq.placeholder = @"QQ";
        
        self.birthPlace = [UIButton buttonWithType:UIButtonTypeSystem];
        self.birthPlace.backgroundColor = [UIColor cyanColor];
        [self.birthPlace addTarget:self action:@selector(goSth:) forControlEvents:UIControlEventTouchUpInside];
        [self.birthPlace setTitle:@"出生地" forState:UIControlStateNormal];

        
        
        _view = [[LSYplaceChooseView alloc] initWithFrame:self.view.frame];
        _view.backgroundColor = [UIColor whiteColor];
        UIWindow *win = [[UIApplication sharedApplication].windows lastObject];
        [win addSubview:_view];
        _view.alpha = .7;
        _view.hidden = YES;

        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [LSYcoreDataManager defaults];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(upload)];

    
    
    
    
    [self.view addSubview:self.name];
    [self.view addSubview:self.number];
    [self.view addSubview:self.sex];
    [self.view addSubview:self.phoneNum];
    [self.view addSubview:self.qq];
    [self.view addSubview:self.birthPlace];
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(100);
        make.right.mas_equalTo(-150);
        make.left.mas_equalTo(30);
        make.height.mas_equalTo(40);
    }];
    
    [self.sex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.name);
        make.right.mas_equalTo(-60);
        make.left.equalTo(self.name.mas_right).with.offset(20);
        make.height.equalTo(self.name);
    }];
    
    [self.number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.name.mas_bottom).with.offset(20);
        make.right.mas_equalTo(-60);
        make.left.mas_equalTo(30);
        make.height.equalTo(self.name);
    }];
    
    [self.phoneNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.number.mas_bottom).with.offset(20);
        make.right.mas_equalTo(-60);
        make.left.mas_equalTo(30);
        make.height.equalTo(self.name);
        
    }];
    
    [self.qq mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneNum.mas_bottom).with.offset(20);
        make.right.mas_equalTo(-60);
        make.left.mas_equalTo(30);
        make.height.equalTo(self.name);
        
    }];
    
    [self.birthPlace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.qq.mas_bottom).with.offset(20);
        make.right.mas_equalTo(-60);
        make.left.mas_equalTo(30);
        make.height.equalTo(self.name);
    }];
    
    
//    self
    
    // Do any additional setup after loading the view.
}

- (void)goSth:(UIButton*)button
{
    _view.hidden = NO;
    _view.place = button.titleLabel.text;

}

- (void)upload
{
    
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Member" inManagedObjectContext:self.manager.managedObjectContext];
    
    
    Member *data = [[Member alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    data.name = self.name.text;
    data.sex = self.sex.text;
    data.number = [NSNumber numberWithFloat:self.number.text.floatValue];
    data.qq = [NSNumber numberWithFloat:self.qq.text.floatValue];
    data.phoneNum = [NSNumber numberWithFloat:self.phoneNum.text.floatValue];
    data.birthPlace = self.birthPlace.titleLabel.text;

    [self.manager saveContext];

}

- (void)getView
{
    self.title = @"修改联系人";

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
